import Vue from 'vue';

/**
 * 树形结构转数组
 * @param data 树形结构数组
 * @param parent 上级对象
 * @param level 当前递归层级
 * @param expandedAll 是否展开全部
 */
function treeToArray(data: any[], parent: any, level: number | undefined | null, expandedAll: boolean) {
  let tmp = [] as any[];
  Array.from(data).forEach((record) => {
    if (record.extExpanded === undefined) {
      Vue.set(record, 'extExpanded', expandedAll);
    }
    Vue.set(record, 'extShow', record._expanded);
    if (parent) {
      Vue.set(record, 'extParent', parent);
    }
    let extLevel = 0;
    if (level !== undefined && level !== null) {
      extLevel = level + 1;
    }
    Vue.set(record, 'extLevel', extLevel);
    tmp.push(record);
    if (record.children && record.children.length > 0) {
      const children = DataTransfer.treeToArray(record.children, record, extLevel, expandedAll);
      tmp = tmp.concat(children);
    }
  });
  return tmp;
}

/**
 * 数据转换
 */
class DataTransfer {
  public static treeToArray = treeToArray;
}

export default DataTransfer;
